[20260202] BOJ / P5 / 중앙 트리 / 이강현 #1869
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/7812
🧭 풀이 시간
2일
👀 체감 난이도
✏️ 문제 설명
중앙 정점은 모든 정점으로 이르는 비용의 합이 가장 작은 정점임.
트리가 주어질때, 모든 정점에서 중앙 정점까지의 비용의 합을 구하자.
🔍 풀이 방법
dfs, rerooting
무작위 정점을 하나 정하고 dfs를 한 후에, 루트 노드를 바꿔가며 비용을 재계산함.
⏳ 회고
rerooting이라는 기법을 알지 못해서 공부하고 풀었는데, 현재 두 노드간 관계를 계산하는 과정에서 첫 dfs의 루트를 고정하고 진행했기에 매 순간 dfs를 해야하는거 아닌가라고 생각해서 막혀있었다. 하지만, 전체 노드의 개수를 알기 때문에 전체 노드의 수에서 다음 노드의 하위 노드 수를 빼고 현재 노드와 하위 노드는 서로 상쇄되기때문에 2를 뺀 것이 나머지 노드라는 것을 알게되어 해결할 수 있었다.